package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService{
    @Autowired
    private TravelCommentMongoRepository repository;

    @Override
    public TravelComment save(TravelComment comment) {
        //设置时间
        comment.setCreateTime(new Date());
        //判断是一个游记评论还是回复评论
        if (comment.getType().equals(TravelComment.TRAVEL_COMMENT_TYPE)) {
            //获取refCommentId,查询得到 -->> 回复的ref评论
            Optional<TravelComment> op = repository.findById(comment.getRefComment().getId());
            //不为空就设置为comment的ref评论
            op.ifPresent(entity -> {
                comment.setRefComment(entity);
            });
        }
        //保存到mongodb数据库
        repository.save(comment);
        return comment;
    }

    @Override
    public List<TravelComment> findByTravelId(Long id) {
        return repository.findByTravelId(id);

    }
}
